﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq.Expressions;
using System.Linq.Dynamic;
using WXYW.BLL;
using WXYW.DAL;
using System.Collections;

namespace WXYW
{
    public partial class ManageProvider : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindProviderList();
            }
        }

        private void BindProviderList()
        {            
            ProviderService providerService = new ProviderService();
            List<Provider> providerList = providerService.GetAllSimpleProvider();
            //switch (sortExpression)
            //{
            //    case "ProviderCode":
            //        providerList = providerList.OrderBy(p => p.ProviderCode).ToList();
            //        break;
            //    case "ProviderName":
            //        providerList = providerList.OrderBy(p => p.ProviderName).ToList();
            //        break;
            //    case "Contact1Email":
            //        providerList = providerList.OrderBy(p => p.Contact1Email).ToList();
            //        break;
            //}
            ProviderGridView.DataSource = providerList;
            ProviderGridView.DataBind();
        }

        protected void ProviderGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                ((Label)e.Row.FindControl("IDLbl")).Text = (e.Row.RowIndex + 1).ToString();
            }

            if (e.Row.RowType == DataControlRowType.Pager)
            {
                DropDownList pageDDL = e.Row.FindControl("pageDDL") as DropDownList;
                for (int index = 1; index <= ProviderGridView.PageCount; index++)
                {
                    pageDDL.Items.Add(index.ToString());
                }
                pageDDL.SelectedValue = (ProviderGridView.PageIndex + 1).ToString();
            }
        }

        protected void ProviderGridView_Sorting(object sender, GridViewSortEventArgs e)
        {
            //BindProviderList(e.SortExpression, e.SortDirection);

            ProviderService providerService = new ProviderService();
            IQueryable<Provider> providers = providerService.GetAllSimpleProvider().AsQueryable();
            string sortDirection = "asc";
            if (Session["OrderDirection"] != null)
            {
                sortDirection = Session["OrderDirection"].ToString();
                if (sortDirection.Equals("asc"))
                {
                    Session["OrderDirection"] = "desc";
                }
                else
                {
                    Session["OrderDirection"] = "asc";
                }
            }
            else
            {
                Session["OrderDirection"] = "asc";
            }
            ProviderGridView.DataSource = providers.OrderBy(string.Format("{0} {1}", e.SortExpression, sortDirection)).ToList();
            ProviderGridView.DataBind();
        }

        protected void ProviderGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("Detail"))
            {
                Response.Redirect(string.Format("ProviderDetail.aspx?ID={0}", e.CommandArgument.ToString()));
            }
        }

        protected void ProviderGridView_DataBound(object sender, EventArgs e)
        {
            if (ProviderGridView.BottomPagerRow != null)
            {
                ProviderGridView.BottomPagerRow.Visible = true;
            }
        }

        protected void ProviderGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            if (e.NewPageIndex >= 0 && e.NewPageIndex < ProviderGridView.PageCount)
            {
                ProviderGridView.PageIndex = e.NewPageIndex;
            }
            
            BindProviderList();
        }

        protected IEnumerable ProviderGridView_MustAddARow(System.Collections.IEnumerable data)
        {
            List<Provider> dds = (List<Provider>)data;
            dds.Add(new Provider());
            return dds;
        }

        protected void pageDDL_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList pageDDL = sender as DropDownList;
            ProviderGridView.PageIndex = Convert.ToInt32(pageDDL.SelectedIndex);
            BindProviderList();
        }
    }
}
